package guoanmaker.personal.business.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.personal.business.model.PersonalCampaign;
import guoanmaker.personal.business.repository.PersonalCampaignRepository;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Service
public class PersonalCampaignService implements PersonalCampaignServiceInterface {

	@Autowired
	private PersonalCampaignRepository personalCampaignRepository;
	@PersistenceContext
	EntityManager em;
	public int saveInvoice(String userid,String cid){
//		List<PersonalCampaign> list=personalCampaignRepository.selectCampaignByTelAndCode(tel,code);
//		if(list==null||(list!=null&&list.size()==0)){
//			PersonalCampaign personalCampaign=new PersonalCampaign();
//			personalCampaign.setTel(tel);
//			personalCampaign.setCode(code);
//			personalCampaign.setCreatetime(new Date());
//			personalCampaignRepository.save(personalCampaign);
//			return 1;
//		}
//		return -1;
		String sql ="select count(1) from t_personal_user_coupon where user_id='"+userid+"' and oc_id='"+cid+"'";
		Query nativeQuery = em.createNativeQuery(sql);
		List<Integer> resultList = nativeQuery.getResultList();
		System.out.println(resultList.get(0));
		if ((resultList.get(0)+"").equals("0")) {
			return  1;
		}
		return - 1;
	}
	
}
